package com.hardydou.system.controller;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.hardydou.common.result.Result;
import com.hardydou.common.result.ResultHelper;
import com.hardydou.framework.filestore.AbstractFileStore;
import com.hardydou.framework.filestore.AbstractFileStore.FileType;
import com.hardydou.framework.filestore.AbstractFileStore.BusType;
import com.hardydou.framework.filestore.FileStoreFactory;
import com.hardydou.framework.log.anno.LogMetaInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

/**
 * @author ：hardy
 * @description：工具控制器
 * @date ：Created in 2020/6/14
 * @Copyright ：Hardydou.com
 */
@Controller
@RequestMapping("/tools")
@Api(tags = "辅助工具")
@LogMetaInfo(module = "辅助工具")
public class ToolsController {

    @Autowired
    FileStoreFactory fileStoreFactory;

    @ApiIgnore
    @GetMapping("")
    public String index() {
        return "tools/index";
    }

    @PostMapping("/qrcode")
    @ResponseBody
    public Result qrcode(String content, int w, int h) throws IOException {
        Result<Map<String, Object>> result;
        AbstractFileStore fileStore = fileStoreFactory.getFileStore(FileType.IMAGE, BusType.QRCODE, "jpg");
        OutputStream output = fileStore.createFile();
        QrCodeUtil.generate(content, w, h, ImgUtil.IMAGE_TYPE_JPEG, output);
        result = ResultHelper.ok();
        result.getData().put("url", fileStore.getReqPath());
        return result;
    }

}
